<?php


/**
 * @author fyl
 * Date: 16-3-29 下午2:59
 */

class IndexService extends BaseService
{
    private static $self       =   NULL;
    private $mAdmin             =  NULL;
    private $mAds               =  NULL;
    private $mLog               =  NULL;
    private $mProject           =  NULL;
    private $mInvestRecord      =  NULL;
    private $mTicketRecord      =  NULL;
    private $mRechargeRecord    =  NULL;
    private $mWithdrawRecord    =  NULL;
    private $mBankManage        =  NULL;
    private $mUser              =  NULL;
    private $mNews              =  NULL;
    private $mAbutPartner       =  NULL;
    private $mAbutTeam          =  NULL;
    private $mAbutRecruit       =  NULL;
    private $mCompany           =  NULL;
    private $mUserExtend        =  NULL;
    private $mInvitie           =  NULL;
    private $mMessage           =  NULL;
    private $mLookUser          =  NULL;
    private $mLockLogin         =  NULL;
    private $mBusinessPic       =  NULL;
    private $mExperRecord       =  NULL;
    private $mExperScale        =  NULL;
    private $mLookWithdraw      =  NULL;
    private $mLineChart         =  NULL;




    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }
        return self::$self;
    }


    public function __construct()
    {
        $this->mAdmin             =   new Involved_AdminModel();
        $this->mAds               =   new Involved_AdsModel();
        $this->mLog               =   new Involved_LogModel();
        $this->mProject           =   new Involved_ProjectModel();
        $this->mInvestRecord      =   new Involved_InvestRecordModel();
        $this->mTicketRecord      =   new Involved_TicketRecordModel();
        $this->mRechargeRecord    =   new Involved_RechargeRecordModel();
        $this->mWithdrawRecord    =   new Involved_WithdrawRecordModel();
        $this->mBankManage        =   new Involved_BankManageModel();
        $this->mUser              =   new Involved_UserModel();
        $this->mNews              =   new Involved_NewsModel();
        $this->mAbutPartner       =   new Involved_AbutPartnerModel();
        $this->mAbutTeam          =   new Involved_AbutTeamModel();
        $this->mAbutRecruit       =   new Involved_AbutRecruitModel();
        $this->mCompany           =   new Involved_CompanyModel();
        $this->mUserExtend        =   new Involved_UserExtendModel();
        $this->mInvitie           =   new Involved_InvitieModel();
        $this->mMessage           =   new Involved_MessageModel();
        $this->mLookUser          =   new Involved_LookUserModel();
        $this->mLockLogin         =   new Involved_LockLoginModel();
        $this->mBusinessPic       =   new Involved_BusinessPicModel();
        $this->mExperRecord       =   new Involved_ExperRecordModel();
        $this->mExperScale        =   new Involved_ExperScaleModel();
        $this->mLookWithdraw      =   new Involved_LookWithdrawModel();
        $this->mLineChart         =   new Involved_LineChartModel();

    }


    /**
     * 登陆验证
     * @param $user
     * @return mixed
     * @throws Exception
     */
    public function VerifyLogin($user)
    {
        #此处只做简单验证
        if(empty($user->username) || empty($user->password)){
            throw new Exception('必填项不能为空', ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        #数据验证
        $user_info = $this->mUser->getUserInfo($user->username,md5($user->password));

        if(!$user_info){
            throw new Exception('用户名或密码错误', ProfessionErrorCodeEnum::ERROR_PASSWORD_WRONG);
        }
        #存储SESSION

//        if(preg_match('/^1[34578]\d{9}$/',$user->username)){
//            Session::put(UserEnum::USER_INFO_USER_NAME, $user_info->mobile);
//        }else{
//
//        }
        Session::put(UserEnum::USER_INFO_USER_NAME, $user_info->mobile);

        Session::put(UserEnum::USER_INFO_USER_ID, $user_info->user_id);
        Session::put(UserEnum::USER_INFO_SESSION_KEY, $user_info);
        Session::put('times',time());
        return $user_info;

    }

    /**
     * 验证用户名
     * @param $paramObj
     * @return mixed
     */
    public function JsCheckUser($paramObj)
    {
        return $this->mUser->JsCheckUser($paramObj->username);
    }

    /**
     * 验证密码
     * @param $paramObj
     * @return mixed
     */
    public function JsCheckPwd($paramObj)
    {
        return $this->mUser->JsCheckPwd($paramObj->username,md5($paramObj->pwd));
    }

    /**
     *查询登陆错误信息
     * @param $user_name
     * @param $mobile
     * @return mixed
     */
    public function LoginTime($user_name,$mobile)
    {
        return $this->mLockLogin->LoginTime($user_name,$mobile);
    }

    /**
     * 登陆错误插入信息
     * @param $aData
     * @return int
     */
    public function LoginCount($aData)
    {
        return $this->mLockLogin->insert($aData);
    }

    /**
     * 登陆错误修改信息
     * @param $aData
     * @return bool
     */
    public function LoginCountA($aData)
    {
        return $this->mLockLogin->update($aData,['user_name'=>$aData['user_name']]);

    }

    /**
     * 成功后删除记录信息
     * @param $user_name
     * @return int
     */
    public function LoginCountDel($user_name)
    {
        return $this->mLockLogin->delete(['user_name'=>$user_name]);

    }

    /**
     * 注册验证
     * @param $paramObj
     * @return int
     * @throws Exception
     */
    public function VerifyRegister($paramObj)
    {
        #参数验证
        if(empty($paramObj->user_name) || empty($paramObj->pwd) || empty($paramObj->mobile) || empty($paramObj->captcha)){
            throw new Exception('必填项不能为空', ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }

        #手机号验证
        if (!preg_match('/^1[34578]\d{9}$/', $paramObj->mobile)) {
            throw new Exception("手机号码错误", ProfessionErrorCodeEnum::ERROR_USER_MOBILE_FAILURE);
        }

        #手机号是否已注册
        $mobile = $this->mUser->JsCheckMobile($paramObj->mobile);
        if ($mobile) {
            throw new Exception("手机号已注册", ProfessionErrorCodeEnum::ERROR_ACCOUNT_EXIST);
        }

        #判断短信验证
        $info =session_get(UserEnum::CLIENT_NEWS_CAPTCHA);
        if ($info['news_captcha']!=$paramObj->news_captcha) {
            throw new Exception("短信验证码有误", ProfessionErrorCodeEnum::ERROR_ACCOUNT_EXIST);
        }

        #验证码
        if(strtolower($paramObj->captcha) != strtolower(Session::get('ypt_Captcha'))){
            throw new Exception('验证码错误', ProfessionErrorCodeEnum::ERROR_VERIFICATION_CODE_WRONG);
        }

        #注册用户
        $aData = [
            'user_name'          =>     $paramObj->user_name,
            'password'           =>     md5($paramObj->pwd),
            'mobile'             =>     $paramObj->mobile,
            'avatar'             =>     './resource/img/user_img.png',
        ];

        $user_id = $this->mUser->insert($aData);

        if($user_id){
            if(!empty($paramObj->invite_mobile)){
                $invite = $this->mUser->JsCheckInvite($paramObj->invite_mobile);
                if($invite){
                    $aData2 = [
                        'bu_id'      => $user_id,
                        'u_id'      =>  $invite->user_id,
                        'reward'     =>  '100',
                        'datetime'   =>  '30',
                        'time'       =>  time(),
                        'access'     =>  '注册获取',
                        'statue'     =>  1,
                    ];
                    $this->mInvitie->insert($aData2);
                    $aData3 = [
                        'u_id'     =>  $user_id,
                        'reward'    =>  '100',
                        'datetime'   =>  '30',
                        'time'      =>  time(),
                        'access'    =>  '注册获取',
                        'statue'    =>  0,
                    ];
                    $this->mInvitie->insert($aData3);
                }
            }else{
                if(!empty($paramObj->invite_id)){
                    $aData2 = [
                        'bu_id'      =>  $user_id,
                        'u_id'       =>   $paramObj->invite_id,
                        'reward'     =>  '100',
                        'datetime'   =>  '30',
                        'time'       =>  time(),
                        'access'     =>  '注册获取',
                        'statue'     =>  0,
                    ];
                    $this->mInvitie->insert($aData2);
                    $aData3 = [
                        'u_id'     =>  $user_id,
                        'reward'    =>  '100',
                        'datetime'   =>  '30',
                        'time'      =>  time(),
                        'access'    =>  '注册获取',
                        'statue'    =>  0,
                    ];
                    $this->mInvitie->insert($aData3);
                }else{
                    $aData2 = [
                        'u_id'     =>  $user_id,
                        'reward'    =>  '100',
                        'datetime'   =>  '30',
                        'time'      =>  time(),
                        'access'    =>  '注册获取',
                        'statue'    =>  0,
                    ];
                    $this->mInvitie->insert($aData2);
                }
            }
            $aData3=[
                'scale'      =>  '10000',
                'time'       =>  time(),
                'datetime'   =>  '30',
                'access'     =>  '注册获取',
                'user_id'    =>  $user_id,
                'statue'     =>   0,
            ];

            $this->mExperScale->insert($aData3);
            $this->mUserExtend->insert(['user_id'=>$user_id]);
        }else{
            throw new Exception('注册失败');
        }

    }

    /**
     * js验证用户名
     * @param $paramObj
     * @return mixed
     */
    public function JsCheckUserName($paramObj)
    {
        return $this->mUser->JsCheckUsername($paramObj->username);
    }

    /**
     * 验证手机号
     * @param $paramObj
     * @return mixed
     */
    public function JsCheckMobile($paramObj)
    {
        return $this->mUser->JsCheckMobile($paramObj->mobile);
    }


    public function JsCheckCaptcha($paramObj)
    {
        if(strtolower($paramObj->captcha) == strtolower(Session::get('ypt_Captcha'))){
            return 1;
        }else{
            return 2;
        }
    }

    /**
     * 验证邀请人手机号
     * @param $paramObj
     * @return mixed
     */
    public function JsCheckInvite($paramObj)
    {
        return $this->mUser->JsCheckInvite($paramObj->invite_mobile);
    }


    /**
     * 发送短信验证
     * @param $paramObj
     * @return string
     */
    public function VerifyNews($paramObj)
    {
        $info =session_get(UserEnum::CLIENT_NEWS_CAPTCHA);

        if(time()-$info['time']<60&&$paramObj->mobile==$info['mobile']){
            return 10001;
        }else{
            #接受参数
            $aData["mobile"] = $paramObj->mobile;
            #生成随机验证码
            $news_captcha = substr(str_shuffle("012345678901234567890123456789"), 0, 6);
            #发送参数
            $data = $this->mLog->sendTemplateSMS($paramObj->mobile,array($news_captcha,'10'),94059);
            if($data == 10000){
                $aData['time'] = time();
                $aData['news_captcha'] = $news_captcha;
                Session::put(UserEnum::CLIENT_NEWS_CAPTCHA,$aData);
                return 10000;
            }elseif($data == 160040){
                return 160040;
            }

        }
    }

    /**
     * 短信验证
     * @param $paramObj
     * @return int
     */
    public function JsCheckNews($paramObj)
    {
        $info =session_get(UserEnum::CLIENT_NEWS_CAPTCHA);
        $time =time()-$info['time'];
        if($info['news_captcha'] == $paramObj->news_captcha && $info['mobile'] == $paramObj->mobile){
            if($time<=600){
                return 1;
            }else{
                return 2;
            }
        }else{
            return  3;
        }
    }

    /**
     * 忘记密码短信验证
     * @param $paramObj
     * @return int
     */
    public function JsVerifyUserNews($paramObj)
    {
        $info =session_get(UserEnum::CLIENT_NEWS_CAPTCHA);
        $time =time()-$info['time'];
        if($info['news_captcha'] == $paramObj->news_captcha)
        {
            if($time<=600){
                return 1;
            }else{
                return 2;
            }
        }else{
            return 3;
        }
    }

    /**
     * 忘记密码验验证
     * @param $paramObj
     * @return int
     */
    public function JsVerifyUserCaptcha($paramObj)
    {
        if(strtolower($paramObj->captcha) != strtolower(Session::get('ypt_Captcha'))) {
            return 2;
        }
    }

    /**
     * 忘记密码手机验证
     * @param $paramObj
     * @return int
     */
    public function JsVerifyUserMobile($paramObj)
    {
        return $this->mUser->JsVerifyUserMobile($paramObj->mobile);
    }

    /**
     * 忘记密码手机验证
     * @param $paramObj
     * @return mixed
     * @throws Exception
     */
    public function VerifyUserMobile($paramObj)
    {
        #验证码
        if(strtolower($paramObj->captcha) != strtolower(Session::get('ypt_Captcha'))){
            throw new Exception('验证码错误', ProfessionErrorCodeEnum::ERROR_VERIFICATION_CODE_WRONG);
        }
        return $this->mUser->JsVerifyUserMobile($paramObj->mobile);
    }

    /**
     * 重置密码
     * @param $paramObj
     * @return bool
     */
    public function UserSetPwd($paramObj)
    {
        $info =session_get(UserEnum::CLIENT_NEWS_CAPTCHA);
        $aData['password'] = md5($paramObj->new_pwd);
        return $this->mUser->update($aData,['mobile' => $info['mobile']]);

    }

    public function VerifySetPwd($paramObj)
    {
        $info =session_get(UserEnum::CLIENT_NEWS_CAPTCHA);
        return $this->mUser->VerifySetPwd(md5($paramObj->new_pwd),$info['mobile']);
    }

    /**
     * 广告位列表
     */
    public function AdsList()
    {
        return $this->mAds->AdsList();
    }

    /**
     * 用户列表
     * @return mixed
     */
    public function ManageUser()
    {
        return $this->mUser->ManageUserList();

    }

    /**
     * ajax投资支付密码
     * @return mixed
     */
   public function AjaxProjectPay($paramObj)
    {
        $asda=session_get(UserEnum::USER_INFO_SESSION_KEY);
        if (!empty($asda->user_id )) {
            if (empty($paramObj->pwd)){
                $mess['code'] = '2';
                $mess['mess'] = '密码不能为空！！!';
                return $mess;
             }
            $pay_num    = $asda->pay_num;//随机数
            $select     =$paramObj->select;//优惠券
            $tmpArr     = explode('_',$select);
            $select     =$tmpArr[0];//优惠券
            if(!empty($select)){$select;}else{$select=0;}
            //$selectid   =$tmpArr[1];//使用优惠券id
            if(!empty($tmpArr[1])){ $selectid   =$tmpArr[1];}
            $moneys     =$paramObj->moneys;//支付金额
            $pwd        =md5(md5($paramObj->pwd).$pay_num);
            $UserE      =$this->mUserExtend->UserExtendone($asda->user_id);//获取总金额
            $amount     =$UserE->user_balance;//账户总金额
            if(!empty($amount) && !empty($moneys)){$in_amo =($amount-$moneys)+$select;}
            $LookUsers  =$this->mLookUser->lockuser($asda->user_id);//获取人是不是本冻结
            if(!empty($LookUsers)){//如果被冻结，看是否到解锁时间
                $LookUsersa  =$this->mLookUser->lockuserone($asda->user_id);//获取人是不是本冻结
                if(time()<$LookUsersa->locktime){
                    //return('{"success":false,"msg":"对不起，您的账户已被锁定,请于'.date('Y-m-d H:i:s',$LookUsersa->locktime).'后尝试登录！"}');
                    $mess['code'] = '2';
                    $mess['mess'] = '对不起，您的账户已被锁定,请于'.date('Y-m-d H:i:s',$LookUsersa->locktime).'后尝试登录！';
                    return $mess;
                }else{
                    $LookUseraaaa=$this->mLookUser->delete(['user_id'=>$asda->user_id]);
                }
            }
            if ($pwd==$asda->pay_pwd){
                 $aData= array(
                    'user_balance'  =>$in_amo,//投资完的资金
                );
                //收益金额
                $yield_money=$this->mUser->Algorithm($moneys,$paramObj->limit,$paramObj->yield_rate);
                $rdata=array(
                    'project_id'    =>$paramObj->project_id,
                    'type'          =>$paramObj->type,
                    'user_id'       =>$asda->user_id,
                    'project_name'  =>$paramObj->project_name ,
                    'invest_money'  =>$moneys,
                    'yield_rate'    =>$paramObj->yield_rate ,
                    'limit'         =>$paramObj->limit ,
                    'invest_time'   =>time(),
                    'yield_money'   =>round($yield_money,2),
                    'status'        =>2
                );
                //删除优惠券
                if(!empty($tmpArr[1])){
                    $Invitiesa =$this->mInvitie->delete(['t_id'=>$selectid,'u_id'=>$asda->user_id]);
                }
                $dataextend= $this->mUserExtend->update($aData,['user_id' =>$asda->user_id]);
                $dataRecord= $this->mInvestRecord->insert($rdata);//投资记录表
                //柱状图
                $time3 = date('Y-m-d', time());//获得当前时间戳 $v->datetime
                $time2 = strtotime(date('Y-m-d', strtotime("$time3 + $paramObj->limit day")));//到期时间
                $countmoney=$this->mUser->CountAlgorithm($moneys,$paramObj->limit,$paramObj->yield_rate);
                $countmoney=round($countmoney,2);
                $cart=array(
                    'year'          =>  date('Y', time()),
                    'month'         =>  date('n', time()),
                    'wait_money'    =>  $countmoney,
                    'user_id'       =>  $asda->user_id ,
                    );
                   $LineChart  =$this->mLineChart->linechartpay($asda->user_id,date('Y', time()),date('n', time()));
                   if(!empty($LineChart )){
                     $LineChartone  =$this->mLineChart->linechartone($asda->user_id,date('Y', time()),date('n', time()));
                     $countmoneys= $countmoney+$LineChartone->wait_money;
                     $cart=array('wait_money'    =>  $countmoneys,);
                     $this->mLineChart->update($cart,['id' =>$LineChartone->id]);
                     // print_r($LineChartone);
                   }else{
                     $this->mLineChart->insert($cart);//柱状图
                   }
                if (!empty($dataRecord)&&!empty($dataextend)) {
                    //return('{"success":true,"msg":"交易成功!."}');
                    $mess['code'] = '1';
                    $mess['mess'] = '交易成功';
                    return $mess;
                }
            }else{
                $LookUsersa  =$this->mLookUser->lockuserone($asda->user_id);//获取本人有没有输入
                if(!empty($LookUsersa->number)){
                    $count = $LookUsersa->number;
                        if($count<= 4){
                            $count++;
                            $logins=$count;
                            $array=array(
                                'number'    =>$logins,
                                'user_id'   =>$asda->user_id,
                                'locktime'  =>time(),
                                'ip'        =>$_SERVER['REMOTE_ADDR'],
                            );
                            if ($count<1) {
                                $LookUserd= $this->mLookUser->insert($array);
                            }
                              if ($count>0&&$count<=4) {
                                $LookUserd= $this->mLookUser->update($array,['id' => $LookUsersa->id]);
                            }

                        }
                        if($count == 5){
                            $data4['number'] = $count;
                            $data4['user_id'] = $asda->user_id;
                            $data4['locktime'] = time()+60*30;
                            $data4['ip'] = $_SERVER['REMOTE_ADDR'];
                            $LookUserd= $this->mLookUser->update($data4,['id' => $LookUsersa->id]);
                            if (!empty($LookUserd)) {
                                //return('{"success":false,"msg":"您已经连续5次输入错误密码，账户被锁定30分钟"}');
                                $mess['code'] = '2';
                                $mess['mess'] = '您已经连续5次输入错误密码，账户被锁定30分钟';
                                return $mess;
                            }
                        }
                        //return('{"success":false,"msg":"密码错误,这是你第'.$count.'次,一共5次机会"}');
                        $mess['code'] = '3';
                        $mess['mess'] = '密码错误,这是你第'.$count.'次,一共5次机会';
                        return $mess;
                    }else{
                    // cookie('logins', $user . '_1');
                     $logins=1;
                     $array=array(
                                'number'    =>$logins,
                                'user_id'   => $asda->user_id,
                                'locktime'  =>time(),
                                'ip'        =>$_SERVER['REMOTE_ADDR'],
                            );
                    $LookUserd= $this->mLookUser->insert($array);
                    }
                if (empty($LookUsersa->number)) {
                   //return('{"success":false,"msg":"密码错误,这是你第1次,一共5次机会"}');
                    $mess['code'] = '3';
                    $mess['mess'] = "密码错误,这是你第1次,一共5次机会";
                    return $mess;
                    }

            }
        }else{
            //return('{"success":false,"msg":"你还没有登录,请登录！！！"}');
            $mess['code'] = '2';
            $mess['mess'] = "你还没有登录,请登录";
            return $mess;
        }

    }
   /**
     * 体验标支付动作
     */
    public  function AjaxSubmitExperient($paramObj){
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        $t_id=$paramObj->t_id;
        if(!empty($user_id)){
            if(empty($paramObj->experience)){
                $mess['code'] = '2';
                $mess['mess'] = '金额不能为空';
                return $mess;
            }
            if(empty($paramObj->experience)||empty($paramObj->yersl)||empty($paramObj->nums)){
                $mess['code'] = '2';
                $mess['mess'] = '对不起,参数错误！！！';
                return $mess;
            }
            $data=array(
                'project_name'  =>'体验标',
                'exper_money'   =>$paramObj->experience,//金额
                'exper_annua'   =>$paramObj->yersl,//年利率
                'exper_nums'    =>$paramObj->nums,//期限
                'exper_time'    =>time(),
                'user_id'       =>$user_id,
            );
            $ExperRecord= $this->mExperRecord->insert($data);
            $updata=array(
                'statue'=>2,
            );
            $Scale=$this->mExperScale->update($updata,['t_id'=>$t_id]);
            if(!empty($ExperRecord)&&!empty($Scale)){
                $mess['code'] = '1';
                $mess['mess'] = '投资成功！！！';
                return $mess;
            }
        }else{
            $mess['code'] = '2';
            $mess['mess'] = '你还没有登录,请登录！！！';
            return $mess;
        }
        //return   $user_id;
    }
    public function ExperRecord()
    {
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return $this->mExperRecord->ExperRecord($user_id);
    }
    /**
     * 项目央票通列表
     */
    public function YptProjectList($project_id=null){
        if(empty($project_id)and empty($type)){
            $list = $this->mProject->YptProjectList($project_id);
            $company_name = $this->Relevance($list,'company_name'); //获取公司名称数组
            $companylist = $this->mCompany->CompanyList($company_name);//获取公司列表
            $company = $this->VelToKey($companylist,'company_name');
            //获取项目已投资金额
            $all_prpject_id = $this->Relevance($list,'project_id'); //获取项目名称数组
            $already_money = $this->mInvestRecord->AlreadyMoney($all_prpject_id);//根据ID获取项目投资金额
            $already = $this->VelToKey($already_money,'project_id');
            $time3 = strtotime(date('Y-m-d H:i:s', time()));//获得当前时间戳
            foreach($list as &$v){
                if(isset($company)){
                    $v->rank = $company[$v->company_name]->rank;
                    $v->logo_img = $company[$v->company_name]->logo;
                    $v->times=$time3-$v->end_time;
                    if(empty($already[$v->project_id])){
                        $v->percentage = "0%";
                        $v->already_money   =$v->project_scale;
                    }else{

                        $v->percentage = number_format($already[$v->project_id]->money/$v->project_scale*100,2)."%";
                        $v->already_money=$v->project_scale-$already[$v->project_id]->money;

                    }
                }
            }
            //print_r($list);

        }else{
            $list = $this->mProject->YptProjectList($project_id);
            $company_name = $this->Relevance($list,'company_name'); //获取公司名称数组
            $companylist = $this->mCompany->CompanyList($company_name);//获取公司列表
            $company = $this->VelToKey($companylist,'company_name');
            //获取项目已投资金额
            $all_prpject_id = $this->Relevance($list,'project_id'); //获取项目ID称数组
            $already_money = $this->mInvestRecord->AlreadyMoney($all_prpject_id);//根据ID获取项目投资金额
            $already = $this->VelToKey($already_money,'project_id');
            //获取合作伙伴信息
            $all_prpject_id = $this->Relevance($list,'partner_id'); //获取项目ID称数组
            $partnerlist = $this->mAbutPartner->GetPartnerId($all_prpject_id);//根据ID获取项目合作商信息
            $partner = $this->VelToKey($partnerlist,'partner_id');
            $time3 = strtotime(date('Y-m-d H:i:s', time()));//获得当前时间戳
            foreach($list as &$v){
                if(isset($company) and !empty($partner)){
                    $v->rank = $company[$v->company_name]->rank;
                    $v->plate = $company[$v->company_name]->plate;
                    $v->brief = $company[$v->company_name]->brief;
                    $v->stock_num = $company[$v->company_name]->stock_num;
                    $v->partner_id = $partner[$v->partner_id]->partner_name;
                    $v->logo_img = $company[$v->company_name]->logo;
                    $v->times=$time3-$v->end_time;
                    if(empty($already[$v->project_id])){
                        $v->percentage = "0%";
                        $v->already_money   =$v->project_scale;
                    }else{
                        $v->percentage = number_format($already[$v->project_id]->money/$v->project_scale*100,2)."%";
                        $v->already_money=$v->project_scale-$already[$v->project_id]->money;
                    }
                }
            }
        }

        return $list;
    }



    /**
     * 项目上市通列表
     */
    public function SstProjectList(){
        $list = $this->mProject->SstProjectList();
        if(!empty($list)){
            $company_name = array();
            foreach($list as $v){
                if(!in_array($v->company_name,$company_name)){
                    $company_name[] = $v->company_name;
                }
            }
            $companylist = $this->mCompany->CompanyList($company_name);
            $company = $this->VelToKey($companylist,'company_name');

            $all_prpject_id = $this->Relevance($list,'project_id'); //获取公司名称数组
            $already_money = $this->mInvestRecord->AlreadyMoney($all_prpject_id);//根据ID获取项目投资金额
            $already = $this->VelToKey($already_money,'project_id');
            $time3 = strtotime(date('Y-m-d H:i:s', time()));//获得当前时间戳
            foreach($list as &$v){
                if(isset($companylist)){
                    $v->plate = $company[$v->company_name]->plate;
                    $v->times=$time3-$v->end_time;
                    if(empty($already[$v->project_id])){
                        $v->percentage      = "0%";
                        $v->already_money   =$v->project_scale;
                    }else{
                        if(empty($already[$v->project_id])){
                            $already[$v->project_id]=0;
                        }
                        $v->percentage = number_format($already[$v->project_id]->money/$v->project_scale*100,2)."%";
                        $v->already_money=$v->project_scale-$already[$v->project_id]->money;
                    }
                }
            }
        }
        return   $list;

    }
    public function YptProjectInfo($project_id=null)
    {
        if(empty($project_id)and empty($type)){
            $list = $this->mProject->YptProjectInfo($project_id);
            $company_name = $this->Relevance($list,'company_name'); //获取公司名称数组
            $companylist = $this->mCompany->CompanyList($company_name);//获取公司列表
            $company = $this->VelToKey($companylist,'company_name');
            //获取项目已投资金额
            $all_prpject_id = $this->Relevance($list,'project_id'); //获取项目名称数组
            $already_money = $this->mInvestRecord->AlreadyMoney($all_prpject_id);//根据ID获取项目投资金额
            $already = $this->VelToKey($already_money,'project_id');
            $time3 = strtotime(date('Y-m-d H:i:s', time()));//获得当前时间戳
            foreach($list as &$v){
                if(isset($company)){
                    $v->rank = $company[$v->company_name]->rank;
                    $v->logo_img = $company[$v->company_name]->logo;
                    $v->times=$time3-$v->end_time;
                    if(empty($already[$v->project_id])){
                        $v->percentage = "0%";
                        $v->already_money   =$v->project_scale;
                    }else{
                        $v->percentage = number_format($already[$v->project_id]->money/$v->project_scale*100,2)."%";
                        $v->already_money=$v->project_scale-$already[$v->project_id]->money;

                    }
                }
            }
            //print_r($list);

        }else{
            $list = $this->mProject->YptProjectInfo($project_id);
            $company_name = $this->Relevance($list,'company_name'); //获取公司名称数组
            $companylist = $this->mCompany->CompanyList($company_name);//获取公司列表
            $company = $this->VelToKey($companylist,'company_name');
            //获取项目已投资金额
            $all_prpject_id = $this->Relevance($list,'project_id'); //获取项目ID称数组
            $already_money = $this->mInvestRecord->AlreadyMoney($all_prpject_id);//根据ID获取项目投资金额
            $already = $this->VelToKey($already_money,'project_id');
            //获取合作伙伴信息
            $all_prpject_id = $this->Relevance($list,'partner_id'); //获取项目ID称数组
            $partnerlist = $this->mAbutPartner->GetPartnerId($all_prpject_id);//根据ID获取项目合作商信息
            $partner = $this->VelToKey($partnerlist,'partner_id');
            $time3 = strtotime(date('Y-m-d H:i:s', time()));//获得当前时间戳
            foreach($list as &$v){
                if(isset($company) and !empty($partner)){
                    $v->rank = $company[$v->company_name]->rank;
                    $v->plate = $company[$v->company_name]->plate;
                    $v->brief = $company[$v->company_name]->brief;
                    $v->stock_num = $company[$v->company_name]->stock_num;
                    $v->partner_id = $partner[$v->partner_id]->partner_name;
                    $v->logo_img = $company[$v->company_name]->logo;
                    $v->times=$time3-$v->end_time;
                    if(empty($already[$v->project_id])){
                        $v->percentage = "0%";
                        $v->already_money   =$v->project_scale;
                    }else{
                        $v->percentage = number_format($already[$v->project_id]->money/$v->project_scale*100,2)."%";
                        $v->already_money=$v->project_scale-$already[$v->project_id]->money;
                    }
                }
            }
        }

        return $list;
        //return $this->mProject->YptProjectInfo();
    }



    public function SstProjectInfo()
    {
        $list = $this->mProject->SstProjectInfo();
        if(!empty($list)){
            $company_name = array();
            foreach($list as $v){
                if(!in_array($v->company_name,$company_name)){
                    $company_name[] = $v->company_name;
                }
            }
            $companylist = $this->mCompany->CompanyList($company_name);
            $company = $this->VelToKey($companylist,'company_name');

            $all_prpject_id = $this->Relevance($list,'project_id'); //获取公司名称数组
            $already_money = $this->mInvestRecord->AlreadyMoney($all_prpject_id);//根据ID获取项目投资金额
            $time3 = strtotime(date('Y-m-d H:i:s', time()));//获得当前时间戳
            $already = $this->VelToKey($already_money,'project_id');
            foreach($list as &$v){
                if(isset($companylist)){
                    $v->plate = $company[$v->company_name]->plate;
                    $v->times=$time3-$v->end_time;
                    if(empty($already[$v->project_id])){
                        $v->percentage      = "0%";
                        $v->already_money   =$v->project_scale;
                    }else{
                        $v->percentage = number_format($already[$v->project_id]->money/$v->project_scale*100,2)."%";
                        $v->already_money=$v->project_scale-$already[$v->project_id]->money;
                    }
                }
            }
        }
        return   $list;
        //return $this->mProject->SstProjectInfo();
    }



    /**
     * 查询一条项目
     */
    public function SelectOneProject($paramObj){
        $aData =  $paramObj->project_id;
        return $this->mProject->GetProjectOne($aData);
    }

    /**
     * 项目总数
     */
    public function ProjectNum(){
        return $this->mProject->ProjectNum();
    }

    /**
     * 项目总规模
     */
    public function ProjectScale(){
        return $this->mProject->ProjectScale();
    }



    /**
     * 投资记录分页
     */
    public function InvestRecordListPage($project_id,$page){
        $page2 = $page;
        $list = $this->mInvestRecord->InvestRecordList($project_id,$page);
        $user_id = $this->Relevance($list,'user_id'); //获取用户ID数组
        if(count($user_id)!=0) {
            $user_list = $this->mUser->UserName($user_id);
            $user = $this->VelToKey($user_list, 'user_id');
            foreach ($list as &$v) {
                $v->user_id = $user[$v->user_id]->user_name;
            }
            $count = $this->mInvestRecord->AllInvestNum($project_id);
            $page_num = ceil($count / HostEnum::PAGE_NUM);
            $pageup = $page - 1 > 1 ? $page - 1 : 1;
            $pagedown = $page + 1 < $page_num ? $page + 1 : $page_num;
            //分页列表
            $page_list = "";
            $num = "";
            foreach ($list as $v) {
                $num = $num +1;
                if ($v->status == 1) {
                    $status = "<span style='color: green'>已审核<span>";
                } elseif ($v->status == 2) {
                    $status = "<span style='color: orange'>审核中</span>";
                } else {
                    $status = "<span style='color: red'>未审核</span>";
                };
                if($num!=10){
                    $size = ($page-1).$num;
                }else{
                    $size = $page.'0';
                }
                $page_list .= "<tr><td>$size</td><td>$v->user_id</td><td>$v->invest_money</td><td>$status</td><td>".date('Y-m-d H:i:s', $v->invest_time)."</td></tr>";
            }
            $page = "";
            $page .= "<li><a href='javascript:void(0);' class='page' data-page=$pageup data-project_id=$project_id><span>←</span></a></li>";
            for ($i = 1; $i <= $page_num; $i++) {
                if ($page2 == $i) {
                    $page .= "<li class='active'><a href='javascript:void(0);' class='page'  data-page=$i data-project_id=$project_id >$i</a></li>";
                } else {
                    $page .= "<li><a href='javascript:void(0);' class='page'  data-page=$i data-project_id=$project_id >$i</a></li>";
                }
            }
            $page .= "<li><a href='javascript:void(0);' class='page' data-page=$pagedown data-project_id=$project_id><span>→</span></a></li>";
            $page_all = "<table class='record'><tr><th width='150px;'>序号</th><th width='250px;'>投资人</th><th width='220px;'>投资金额（元）</th><th width='260px;'>投资状态</th><th width='244px;'>投资时间</th></tr>$page_list<ul class='pagination'>$page
</ul></table>";
        }else{
            $page_all = "<table class='record'><tr><th width='150px;'>序号</th><th width='250px;'>投资人</th><th width='220px;'>投资金额（元）</th><th width='260px;'>投资状态</th><th width='244px;'>投资时间</th></table>";
        }
        return  $page_all;
    }

    /**
     * 查询一条投资记录
     */
    public function SelectOneInvestRecord($paramObj){
        $aData =  $paramObj->invest_id;
        return $this->mInvestRecord->GetInvestRecordOne($aData);
    }

    /**
     * 投资总收益
     */
    public function AllInvestRecord(){
        return $this->mInvestRecord->AllInvestRecord();
    }

    /**
     * 投资总笔数
     */
    public function AllInvestNum(){
        return $this->mInvestRecord->AllInvestNum();
    }


    /**
     * 报票记录列表
     */
    public function TicketRecordList(){
        return $this->mTicketRecord->TicketRecordList();
    }


    /**
     * 充值记录列表
     */
    public function RechargeRecordList(){
        return $this->mRechargeRecord->RechargeRecordList();
    }


    /**
     * 提现记录列表
     */
    public function WithdrawRecordList(){
        return $this->mWithdrawRecord->WithdrawRecordList();
    }


    /**
     * 银行卡列表
     */
    public function BankManageList(){
        return $this->mBankManage->BankManageList();
    }


    /**
     * 首页资讯列表
     * @return mixed
     * hyzx行业资讯 ptxw平台新闻 mtbd媒体报道
     */
    public function NewsIndexList()
    {
        $new = array();
        $new['hyzx'] = $this->mNews->NewsIndexSelect(1,1);
        $new['ptxw'] = $this->mNews->NewsIndexSelect(2,1);
        $new['mtbd'] = $this->mNews->NewsIndexSelect(3,1);

        return $new;
    }

    /**
     * 公告列表
     */
    public function AnnouncementList(){
        return $this->mNews->AnnouncementListSelect(2);
    }

    /**团队列表
     * @return mixed
     */
    public function AboutTeam()
    {
        return $this->mAbutTeam->AboutTeam();
    }
    /**
     * 合作伙伴查询
     * @return mixed
     */
    public function AboutPartner()
    {
        return $this->mAbutPartner->AboutPartner();
    }

    /**
     * 合作伙伴列表
     * @return mixed
     */
    public function PartnerList()
    {
        return $this->mAbutPartner->PartnerSelect();
    }

    /**
     * 团队列表
     * @return mixed
     */
    public function TeamList()
    {
        return $this->mAbutTeam->TeamSelect();
    }


    /**
     * 招聘列表
     * @return mixed
     */
    public function RecruitList()
    {
        return $this->mAbutRecruit->RecruitSelect();
    }

    /**
     * 将array的每个val转化成key
     */
    public function VelToKey($arr = array(),$key_name){
        $arrRes = array();
        foreach($arr as $k => &$v){
            $arrRes[$v->$key_name] = $arr[$k];
        }
        return $arrRes;
    }


    /**
     * 项目央票通列表
     */
    public function SSTProjectpay($project_id=null){
        if(empty($project_id)and empty($type)){
            $list = $this->mProject->SSTProjectpay($project_id);
            $company_name = $this->Relevance($list,'company_name'); //获取公司名称数组
            $companylist = $this->mCompany->CompanyList($company_name);//获取公司列表
            $company = $this->VelToKey($companylist,'company_name');
            //获取项目已投资金额
            $all_prpject_id = $this->Relevance($list,'project_id'); //获取项目名称数组
            $already_money = $this->mInvestRecord->AlreadyMoney($all_prpject_id);//根据ID获取项目投资金额
            $already = $this->VelToKey($already_money,'project_id');
            foreach($list as &$v){
                if(isset($company)){
                    $v->rank = $company[$v->company_name]->rank;
                    $v->logo_img = $company[$v->company_name]->logo;
                    if(empty($already[$v->project_id])){
                        $v->percentage = "0%";
                    }else{
                        $v->percentage = number_format($already[$v->project_id]->money/$v->project_scale*100,2)."%";
                        $v->already_money=$v->project_scale-$already[$v->project_id]->money;
                    }
                }
            }
        }else{
            $list = $this->mProject->SSTProjectpay($project_id);
            $company_name = $this->Relevance($list,'company_name'); //获取公司名称数组
            $companylist = $this->mCompany->CompanyList($company_name);//获取公司列表
            $company = $this->VelToKey($companylist,'company_name');
            //获取项目已投资金额
            $all_prpject_id = $this->Relevance($list,'project_id'); //获取项目ID称数组
            $already_money = $this->mInvestRecord->AlreadyMoney($all_prpject_id);//根据ID获取项目投资金额
            $already = $this->VelToKey($already_money,'project_id');
            //获取合作伙伴信息
            $all_prpject_id = $this->Relevance($list,'partner_id'); //获取项目ID称数组
            $partnerlist = $this->mAbutPartner->GetPartnerId($all_prpject_id);//根据ID获取项目合作商信息
            $partner = $this->VelToKey($partnerlist,'partner_id');
            foreach($list as &$v){
                if(isset($company) and !empty($partner)){
                    $v->rank = $company[$v->company_name]->rank;
                    $v->plate = $company[$v->company_name]->plate;
                    $v->brief = $company[$v->company_name]->brief;
                    $v->stock_num = $company[$v->company_name]->stock_num;
                    $v->partner_id = $partner[$v->partner_id]->partner_name;
                    $v->logo_img = $company[$v->company_name]->logo;
                    if(empty($already[$v->project_id])){
                        $v->percentage = "0%";
                    }else{
                        $v->percentage = number_format($already[$v->project_id]->money/$v->project_scale*100,2)."%";
                        $v->already_money=$v->project_scale-$already[$v->project_id]->money;
                    }
                }
            }
        }

        return $list;
    }

    /**
     * 获取所有关联信息
     */
   public  function Relevance($arr = array(),$name)
   {
       $arrRes = array();
       foreach ($arr as $v) {
           if (!in_array($v->$name, $arrRes)) {
               $arrRes[] = $v->$name;
           }
       }
       return $arrRes;
   }

    /**
     * 行业资讯分页
     * @param $page
     * @return string
     */
    public function AboutInfo1($page)
    {

        $count = $this->mNews->AboutInfo1();
        $data = $this->mNews->AboutInfo1Page1($page);
        //总条数
        $total = count($count);
        //每页显示条数
        $page_size = 5;
        //总页数
        $page_num = ceil($total/$page_size);
        //获取当前页
        //$page=isset($_GET['page'])?intval($_GET['page']):1;
        //上一页
        $last_page = $page-1>1?$page-1:1;
        //下一页
        $next_page = $page+1<$page_num?$page+1:$page_num;
        $str = '';
        $str.="<ul class='infor-box-ul'>";
        foreach($data as $v){
            $str.=  "<li>";
            $str.=  "<div class='til'><a href='/about_info_content?news_id=$v->news_id' target='blank'>$v->title</a></div>";
            $str.=  "<time>".date('Y-m-d',$v->created)."</time>";
            $str.=  "<p>".preg_replace("/(\s|\&emsp\;|　|\xc2\xa0)/", "",mb_substr(strip_tags($v->content),0,150)).'...'."</p>";
            $str.=  '</li>';
        }
        $str.="</ul>";
        if(!empty($data)){
            $str .="<ul class='pagination'>";
            $str .= "<li><a href='javascript:void(0);' class='page1' data-page=$last_page ><span>←</span></a></li>";
            for ($i = 1; $i <= $page_num; $i++) {
                if($page == $i){
                    $str .= "<li class='active'><a href='javascript:void(0);' class='page1'  data-page=$i >$i</a></li>";
                }else{
                    $str .= "<li><a href='javascript:void(0);' class='page1'  data-page=$i >$i</a></li>";
                }
            }
            $str .= "<li><a href='javascript:void(0);' class='page1' data-page=$next_page ><span>→</span></a></li>";
            $str .="</ul>";
        }
        return  $str;
    }

    /**
     * 平台新闻分页
     * @param $page
     * @return string
     */
    public function AboutInfo2($page)
    {
        $count = $this->mNews->AboutInfo2();
        $data = $this->mNews->AboutInfo1Page2($page);
        //总条数
        $total = count($count);
        //每页显示条数
        $page_size = 5;
        //总页数
        $page_num = ceil($total/$page_size);
        //获取当前页
        //$page=isset($_GET['page'])?intval($_GET['page']):1;
        //上一页
        $last_page = $page-1>1?$page-1:1;
        //下一页
        $next_page = $page+1<$page_num?$page+1:$page_num;
        $str = '';
        $str.="<ul class='infor-box-ul'>";
        foreach($data as $v){
            $str.=  "<li>";
            $str.=  "<div class='til'><a href='/about_info_content?news_id=$v->news_id' target='blank'>$v->title</a><span>公告</span></div>";
            $str.=  "<time>".date('Y-m-d',$v->created)."</time>";
            $str.=  "<p>".preg_replace("/(\s|\&emsp\;|　|\xc2\xa0)/", "",mb_substr(strip_tags($v->content),0,150)).'...'."</p>";
            $str.=  '</li>';
        }
        $str.="</ul>";
        if(!empty($data)){
            $str .="<ul class='pagination'>";
            $str .= "<li><a href='javascript:void(0);' class='page2' data-page=$last_page ><span>←</span></a></li>";
            for ($i = 1; $i <= $page_num; $i++) {
                if($page == $i){
                    $str .= "<li class='active'><a href='javascript:void(0);' class='page2'  data-page=$i >$i</a></li>";
                }else{
                    $str .= "<li><a href='javascript:void(0);' class='page2'  data-page=$i >$i</a></li>";
                }
            }
            $str .= "<li><a href='javascript:void(0);' class='page2' data-page=$next_page ><span>→</span></a></li>";
            $str .="</ul>";
        }

        return  $str;
    }

    /**
     * 媒体报道分页
     * @param $page
     * @return string
     */
    public function AboutInfo3($page)
    {
        header("content-type:text/html;charset=utf8");
        $count = $this->mNews->AboutInfo3();
        $data = $this->mNews->AboutInfo1Page3($page);
        //总条数
        $total = count($count);
        //每页显示条数
        $page_size = 5;
        //总页数
        $page_num = ceil($total/$page_size);
        //获取当前页
        //$page=isset($_GET['page'])?intval($_GET['page']):1;
        //上一页
        $last_page = $page-1>1?$page-1:1;
        //下一页
        $next_page = $page+1<$page_num?$page+1:$page_num;
        $str = '';
        $str.="<ul class='infor-box-ul'>";
        foreach($data as $v){
            $str.=  "<li>";
            $str.=  "<div class='til'><a href='/about_info_content?news_id=$v->news_id' target='blank'><i>【媒体报道】</i>$v->title</a></div>";
            $str.=  "<time>".date('Y-m-d',$v->created)."</time>";
            $str.=  "<p>".preg_replace("/(\s|\&emsp\;|　|\xc2\xa0)/", "",mb_substr(strip_tags($v->content),0,150)).'...'."</p>";
            $str.=  '</li>';
        }
        $str.="</ul>";

        if(!empty($data)){
            $str .="<ul class='pagination'>";
            $str .= "<li><a href='javascript:void(0);' class='page3' data-page=$last_page ><span>←</span></a></li>";
            for ($i = 1; $i <= $page_num; $i++) {
                if($page == $i){
                    $str .= "<li class='active'><a href='javascript:void(0);' class='page3'  data-page=$i >$i</a></li>";
                }else{
                    $str .= "<li><a href='javascript:void(0);' class='page3'  data-page=$i >$i</a></li>";
                }
            }
            $str .= "<li><a href='javascript:void(0);' class='page3' data-page=$next_page ><span>→</span></a></li>";
            $str .="</ul>";
        }


        return  $str;
    }


    public function AboutInfoContent($news_id)
    {
        return $this->mNews->AboutInfoContent($news_id);
    }

    /**
     * 商票上传
     * @param $paramObj
     * @return int
     * @throws Exception
     */
    public function UploadImg($paramObj)
    {


        #接受参数
        $paramObj->front  =  QiniuUpload::instance()->upload_files($paramObj->front);
        $paramObj->behind =  QiniuUpload::instance()->upload_files($paramObj->behind);

        $aData = [
            'f_pic'      =>  $paramObj->front,
            'b_pic'      =>  $paramObj->behind,
            'up_time'    =>  time(),
            'user_id'    =>  session_get(UserEnum::USER_INFO_USER_ID),
        ];

        return $this->mBusinessPic->insert($aData);
    }

    /**
     * 出票企业(央票通)
     * @return mixed
     */
    public function TopEnterYpt()
    {
        return $this->mCompany->TopEnterYpt();
    }
    /**
     * 出票企业(上市通)
     * @return mixed
     */
    public function TopEnterSst()
    {
        return $this->mCompany->TopEnterSst();
    }

    /**
     * 出票央企详情
     * @param $company_id
     * @return mixed
     */
    public function TopEnterArticle($company_id)
    {
        return $this->mCompany->TopEnterArticle($company_id);
    }

    /**
    *可用总金额
    */
    public function   UserExtendone(){
        $user_id = session_get(UserEnum::USER_INFO_USER_ID);
        return  $this->mUserExtend->UserExtendone($user_id);//获取总金额

    }
}